package com.example.cardgame;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;

public class EditInfoActivity extends AppCompatActivity {

    private EditText etName, etStudentId, etPhone, etEmail;
    private RadioGroup rgGender;
    private RadioButton rbMale, rbFemale;
    private CheckBox cbTravel, cbReading, cbGaming, cbShopping;
    private Button btnSave, btnCancel;
    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);

        initViews();
        initData();
        setupClickListeners();
    }

    private void initViews() {
        etName = findViewById(R.id.et_name);
        etStudentId = findViewById(R.id.et_student_id);
        etPhone = findViewById(R.id.et_phone);
        etEmail = findViewById(R.id.et_email);
        rgGender = findViewById(R.id.rg_gender);
        rbMale = findViewById(R.id.rb_male);
        rbFemale = findViewById(R.id.rb_female);
        cbTravel = findViewById(R.id.cb_travel);
        cbReading = findViewById(R.id.cb_reading);
        cbGaming = findViewById(R.id.cb_gaming);
        cbShopping = findViewById(R.id.cb_shopping);
        btnSave = findViewById(R.id.btn_save);
        btnCancel = findViewById(R.id.btn_cancel);
    }

    private void initData() {
        userInfo = (UserInfo) getIntent().getSerializableExtra("userInfo");
        if (userInfo == null) {
            userInfo = new UserInfo();
        }

        populateForm();
    }

    private void populateForm() {
        etName.setText(userInfo.getName());
        etStudentId.setText(userInfo.getStudentId());
        etPhone.setText(userInfo.getPhone());
        etEmail.setText(userInfo.getEmail());

        // 设置性别
        if (userInfo.getGender().equals("男")) {
            rbMale.setChecked(true);
        } else {
            rbFemale.setChecked(true);
        }

        // 设置爱好
        String hobbies = userInfo.getHobbies();
        cbTravel.setChecked(hobbies.contains("旅游"));
        cbReading.setChecked(hobbies.contains("阅读"));
        cbGaming.setChecked(hobbies.contains("游戏"));
        cbShopping.setChecked(hobbies.contains("购物"));
    }

    private void setupClickListeners() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveUserInfo();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }

    private void saveUserInfo() {
        // 获取基本信息
        userInfo.setName(etName.getText().toString());
        userInfo.setStudentId(etStudentId.getText().toString());
        userInfo.setPhone(etPhone.getText().toString());
        userInfo.setEmail(etEmail.getText().toString());

        // 获取性别
        int selectedGenderId = rgGender.getCheckedRadioButtonId();
        if (selectedGenderId == R.id.rb_male) {
            userInfo.setGender("男");
        } else {
            userInfo.setGender("女");
        }

        // 获取爱好
        StringBuilder hobbiesBuilder = new StringBuilder();
        if (cbTravel.isChecked()) hobbiesBuilder.append("旅游, ");
        if (cbReading.isChecked()) hobbiesBuilder.append("阅读, ");
        if (cbGaming.isChecked()) hobbiesBuilder.append("游戏, ");
        if (cbShopping.isChecked()) hobbiesBuilder.append("购物, ");

        String hobbies = hobbiesBuilder.toString();
        if (hobbies.length() > 0) {
            hobbies = hobbies.substring(0, hobbies.length() - 2); // 移除最后的逗号和空格
        }
        userInfo.setHobbies(hobbies);

        // 返回结果
        Intent resultIntent = new Intent();
        resultIntent.putExtra("userInfo", userInfo);
        setResult(RESULT_OK, resultIntent);
        finish();
    }
}